<?php

class ResourceDescModel extends CI_Model{

	private $table = 'resource_desc';

	function __construct(){
		parent::__construct();
	}

	public function add_rec_basic_desc($data){

		$this->db->insert($this->table,$data);
		$id = $this->db->insert_id();
		return $id;
	}


	public function add_res_detail_desc($id,$res_suit,$userId){

		$sql = 'update resource_desc set userId=?, resource_suit=? where id=?';
		$result = $this->db->query($sql,array($userId,$res_suit,$id));
		return $result;
	}




	public function get_all_check_res($userId){

		$sql = 'select * from resource_desc where userId=? and type!=0';
		$result = $this->db->query($sql,array($userId));
		if ($result->num_rows()> 0){
			return $result->result_array();
		}else{
			return array();
		}	
	}

	public function get_admin_check_res(){
		$sql = 'select * from resource_desc where type = 1';
		$result = $this->db->query($sql);
		if ($result->num_rows()> 0){
			return $result->result_array();
		}else{
			return array();
		}
	}

	//获得审核资源的资料
	public function get_check_res_by_id($id){
		$sql = 'select * from resource_desc where id = ?';
		$result = $this->db->query($sql,array($id));
		if ($result->num_rows()> 0){
			return $result->row_array();
		}else{
			return array();
		}
	}

	public function add_admin_pass_advice($id,$new_res_desc,$admin_advice){

		$sql = 'update resource_desc set resource_description=?,admin_advice=?,type=2 where id=?';
		$result = $this->db->query($sql,array($new_res_desc,$admin_advice,$id));
		// $share_resource = $this->db->get('resource_desc')->result();
		return $result;
	}

	public function add_admin_deny_advice($id,$new_res_desc,$admin_advice,$deny_reason){
		$sql = 'update resource_desc set resource_description=?,admin_advice=?,check_words=?,type=3 where id=?';
		$result = $this->db->query($sql,array($new_res_desc,$admin_advice,$deny_reason,$id));
		return $result;
	}

	//获得公共资源的资料
	public function get_res_info_by_id($id){
		$sql = 'select * from resource_desc where id = ?';
		$result = $this->db->query($sql,array($id));
		if ($result->num_rows()> 0){
			return $result->row_array();
		}else{
			return array();
		}
	}

	//判断文件是否已共享
	public function check_share($id){
		$sql = 'select count(*) as count from resource_desc where resource_id=?';
		$result = $this->db->query($sql,array($id));
		$row = $result->row_array();
		if($row['count'] > 0){
			return false;
		}else{
			return true;
		}
	}

	//获取共享资源的ID
	public function get_all_share($userId){
		$sql = 'select * from resource_desc where userId=? and type=2';
		$result = $this->db->query($sql,array($userId));
		return $result->result_array();
	}

	//根据用户ID获取共享资源的个数
	public function get_resource_num($userId){
		$sql = 'select count(*) as count from resource_desc where userId=?';
		$result = $this->db->query($sql,array($userId));
		$row = $result->row_array();
		return $row['count'];
	}


	public function get_all_public_res(){

		$sql = 'select * from resource_desc where type=2';
		$result = $this->db->query($sql);
		if ($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return array();
		}
	}

	public function get_top10(){

		$sql = 'select * from resource_desc order by downloads desc limit 10';
		$result = $this->db->query($sql);
		if ($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return array();
		}
	}


	public function get_subject_top10($subject){

		$sql = 'select * from resource_desc where subject=? order by downloads desc limit 18';
		$result = $this->db->query($sql,array($subject));
		if ($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return array();
		}
	}
	public function remove_share($id){
		$sql = 'delete from resource_desc where id=?';
		$result = $this->db->query($sql,array($id));
		return $result;
	}
}